 package com.example.uit_management;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class JsonParser_Faculty {

	// Json key node
	static final String KEY_facultyID = "facultyID";
	static final String KEY_facultyName = "facultyName";
	// String of node
	static  String facultyID = "";
	static  String facultyName = "";
	// contrustor
	public JsonParser_Faculty() {
		// TODO Auto-generated constructor stub
	}
	public JSONArray GetJSONArray(){		
		String readTwitterFeed = readTwitterFeed();					
			try
	        {
				JSONObject json = new JSONObject(readTwitterFeed);
				JSONArray jsonArray = json.getJSONArray("data");	
				return jsonArray;
	        }
	        catch (Exception je)
	        {	          
	        	return null;
	        }	  				
	}
	public String readTwitterFeed() {
	    StringBuilder builder = new StringBuilder();
	    HttpClient client = new DefaultHttpClient();
	    HttpGet httpGet = new HttpGet("http://uitstudentmanagement.appspot.com/faculty");
	    try {
	        HttpResponse response = client.execute(httpGet);
	        StatusLine statusLine = response.getStatusLine();
	        int statusCode = statusLine.getStatusCode();
	        if (statusCode == 200) {
	            HttpEntity entity = response.getEntity();
	            InputStream content = entity.getContent();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
	            String line;
	            while ((line = reader.readLine()) != null) {
	                builder.append(line);
	            }
	        } else {
	            //Log.e(JsonParser_Faculty.class.toString(), "Failed to download file");
	        }
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return builder.toString();
	}
	public ArrayList<HashMap<String, String>> getMap_Faculty()
	{
		//Log.e(JsonParser_Faculty.class.toString(), "1");
		ArrayList<HashMap<String, String>> ArrayMap = new ArrayList<HashMap<String, String>>();
		//Log.e(JsonParser_Faculty.class.toString(), "2");
		JSONArray jsonArray = GetJSONArray();
		//Log.e(JsonParser_Faculty.class.toString(), "3");
		// looping through all jsonarray nodes 
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				//create new JsonObject	
				//Log.e(JsonParser_Faculty.class.toString(), "Start to add map faculty");
				JSONObject js = jsonArray.getJSONObject(i);							
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();	
				// Get String from json oject
				facultyID =js.getString("name");		
				facultyName =js.getString("facultyName");
				// adding each child node to HashMap key => value				
				map.put(KEY_facultyID, facultyID);		
				map.put(KEY_facultyName, facultyName);		
				ArrayMap.add(map);					
				//Log.e(JsonParser_Faculty.class.toString(), "Finish to add map faculty");
			} catch (Exception e) {
				// TODO: handle exception
				//Log.e(JsonParser_Faculty.class.toString(), "Failed to add map faculty");
			}
			
		}		
		return ArrayMap;
	}	
	/*
	Put data to Google App Engine
	*/
	public void PutDataRegCourse(String courseID, String studentID, String oper){
    	DefaultHttpClient hc=new DefaultHttpClient();
    	ResponseHandler <String> res=new BasicResponseHandler();
    	HttpPost postMethod=new HttpPost("http://uitstudentmanagement.appspot.com/regCourse");
    	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    	//Log.e(JsonParser_Faculty.class.toString(), "Put Data 1");
    	nameValuePairs.add(new BasicNameValuePair("courseID", courseID));
    	//Log.e(JsonParser_Faculty.class.toString(), "Put Data 2");
    	nameValuePairs.add(new BasicNameValuePair("studentID", studentID));
    	//Log.e(JsonParser_Faculty.class.toString(), "Put Data 3");
    	nameValuePairs.add(new BasicNameValuePair("oper", oper));
    	try {
			postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
    	try {
			String response=hc.execute(postMethod,res);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
